//
//  DepositController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/2/11.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit
import Eureka
class ZyaDepositVC: KSFormViewController,getDepositRemainable {
     
    var z_is_open = ""
 
    lazy var topB:UIView = {
        let v = UIView(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 590))
        v.backgroundColor = UIColor(kshex:"#f3f3f3")
         return v
    
    }()
    
    lazy var confirmBtn:UIButton = {
       let v = UIButton()
        v.layer.cornerRadius = 19
        v.layer.masksToBounds = true
         v.setTitleColor(.white, for: .normal)
        v.setTitle("我已转账", for: .normal)
        v.backgroundColor = Theme.primaryColor()
        return v
    }()
    lazy var headBg:UIView = {
        let v = UIView()
         v.backgroundColor = Theme.primaryColor()
        return v
    }()
    lazy var topContainer:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 6
         return v
    }()
    lazy var top_title:UILabel = {
        let v = UILabel()
        v.textColor = .lightGray
        v.font = UIFont.systemFont(ofSize: 15)
        v.text = "保证金余额(元)"
         return v
    }()
    lazy var bottom_title:UILabel = {
        let v = UILabel()
        v.textColor = .black
        v.font = UIFont.boldSystemFont(ofSize: 19)
        v.text = "转入账号"
         return v
    }()
   
    lazy var bottom_content:UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
         v.textColor = .darkGray
        v.font = UIFont.systemFont(ofSize: 17)
        v.text = "--"
         return v
    }()
    lazy var top_content:UILabel = {
        let v = UILabel()
        v.textColor = .darkGray
        v.font = UIFont.boldSystemFont(ofSize: 19)
        v.text = "--"
         return v
    }()
    lazy var bottomContainer:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 6
          v.layer.masksToBounds = true
         return v
    }()
  
    
    
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "转账确认"
        topB.addSubview(headBg)
        headBg.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(topB)
            make.height.equalTo(60)
        }
        
        
        
        topB.addSubview(topContainer)
        topContainer.snp.makeConstraints { (make) in
            make.left.equalTo(topB).offset(10)
            make.right.equalTo(topB).offset(-10)
            make.top.equalTo(topB).offset(10)
            make.height.equalTo(90)
        }
        topContainer.addSubview(top_title)
        top_title.snp.makeConstraints { (make) in
            make.left.equalTo(topContainer).offset(10)
             make.top.equalTo(topContainer).offset(15)
         }
        topContainer.addSubview(top_content)
        top_content.snp.makeConstraints { (make) in
            make.left.equalTo(top_title)
            make.bottom.equalTo(topContainer).offset(-15)
         }
        
        topB.addSubview(bottomContainer)
        bottomContainer.snp.makeConstraints { (make) in
            make.left.right.equalTo(topContainer)
            make.top.equalTo(topContainer.snp.bottom).offset(10)
            make.height.equalTo(220)
        }
        bottomContainer.addSubview(bottom_title)
        bottom_title.snp.makeConstraints { (make) in
            make.centerX.equalTo(bottomContainer)
             make.top.equalTo(bottomContainer).offset(8)
         }
        bottomContainer.addSubview(bottom_content)
        bottom_content.snp.makeConstraints { (make) in
            make.top.equalTo(bottom_title.snp.bottom).offset(8)
            make.left.equalTo(bottomContainer).offset(10)
            make.right.equalTo(bottomContainer).offset(-10)
          }
    
        
        
        
        
        self.tableView.tableHeaderView = topB
  
        
        getDepositValue { (flag,value) in
            self.top_content.text = value
 
        }
        KSNetwork.shared.doRequest(API.getAccountInfo) { (data:BaseResult<OperateResult>) in
            if data.flag == TRUE {
                if let infostr = data.info_object {
                    self.z_is_open = infostr.z_is_open.unwrap
                    let paraph = NSMutableParagraphStyle()
                    //将行间距设置为28
                    paraph.lineSpacing = 5
                    //样式属性集合
                    let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15),
                                      NSAttributedString.Key.paragraphStyle: paraph]
                    
                    let conStr = infostr.content.unwrap.replacingOccurrences(of: "##", with: "\n")
                    let tipStr = infostr.z_is_open == "0" ? "\n请先联系管理员开通保证金账户" : "\n请把保证金转入以上账户，再点击【我已转账】"
                    let sumStr = conStr + tipStr
                    
                    let sAttr = NSMutableAttributedString(string: sumStr,attributes: attributes)
                    sAttr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: (sumStr as NSString).range(of: tipStr))
       
                     self.bottom_content.attributedText = sAttr
                      
                    
                    if self.z_is_open != "0" {
                       
                        self.confirmBtn.addTouchUpInSideAction { _ in
                            alertUtil(self, title: "是否确认已通过转账方式将保证金转入该界面指定账户？", operateS: [nameAction(name: "确认", action: { (_) in
                                self.saveAction()
                            })])
                        } 
                        self.topB.addSubview(self.confirmBtn)
                        self.confirmBtn.snp.makeConstraints { make in
                            make.centerX.equalTo(self.topB)
                            make.width.equalTo(200)
                            make.height.equalTo(38)
                            make.top.equalTo(self.bottomContainer.snp.bottom).offset(20)
                        }
                    }
 
                    
                }
            } else {
                KSToast(data.message)
            }
        }
        
  

        
    }
     
    
    
    // MARK: - 提交
    func saveAction() {
   
         let api = API.saveDepositPaymentInfo
        KSNetwork.shared.doRequest(api) { (data:BaseResult<OperateResult>) in
            if data.flag == TRUE {
                KSToast(data.message.unwrap,duration: 2)
                 
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: CALL_tenderinfo_REFRESH), object: nil)
                self.confirmBtn.layer.borderColor = UIColor.lightGray.cgColor
                self.confirmBtn.setTitleColor(.lightGray, for: .normal)
                self.confirmBtn.isUserInteractionEnabled = false
                
            } else {
                KSToast(data.message.unwrap)
            }
        }
        
    }
}
